package com.yuven.appframework.util.arouter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavigationCallback;
import com.alibaba.android.arouter.facade.template.IProvider;
import com.alibaba.android.arouter.launcher.ARouter;

import java.io.Serializable;
import java.util.Map;

/**
 * 
 *
 * @author :        Yuven
 * @date :          2022/10/17 09:02
 * @desc :
 */
public class ARouterManager {

    /**
     * 获取 IProvider 实现
     * @param clazz
     * @param path
     * @param <T>
     * @return
     */
    public static <T extends IProvider> T getServiceImpl(Class<T> clazz, String path) {
        return (T) ARouter.getInstance().build(path).navigation();
    }

    public static void startActivity(String action) {
        start(null, action, null, null, -1, null);
    }

    public static void startActivity(String action, Map<String, Object> intentParams) {
        start(null, action, intentParams, null, -1, null);
    }

    public static void startActivity(String action, Bundle bundle) {
        start(null, action, null, bundle, -1, null);
    }

    public static void startActivityForResult(Activity activity, String action, int requestCode) {
        start(activity, action, null, null, requestCode, null);
    }

    public static void startActivityForResult(Activity activity, String action, Map<String, Object> intentParams, int requestCode) {
        start(activity, action, intentParams, null, requestCode, null);
    }

    public static void startActivityForResult(Activity activity, String action, Bundle bundle, int requestCode) {
        start(activity, action, null, bundle, requestCode, null);
    }

    private static void start(Context context, String action, Map<String, Object> intentParams, Bundle bundle, int requestCode, NavigationCallback callback) {
        Postcard build = ARouter.getInstance().build(action);
        //interceptorCounter.await(postcard.getTimeout(), TimeUnit.SECONDS);
        build.setTimeout(1);
        if (intentParams != null) {
            setIntentParams(build, intentParams);
        } else if (bundle != null) {
            build.with(bundle);
        }
        if (requestCode < 0) {
            if (callback == null) {
                build.navigation();
            } else {
                build.navigation(context, callback);
            }
        } else {
            if (callback == null) {
                build.navigation((Activity) context, requestCode);
            } else {
                build.navigation((Activity) context, requestCode, callback);
            }
        }
    }

    private static void setIntentParams(Postcard build, Map<String, Object> intentParams) {
        for (Map.Entry<String, Object> next : intentParams.entrySet()) {
            Object obj = next.getValue();
            if (obj instanceof String) {
                build.withString(next.getKey(), (String) next.getValue());
            } else if (obj instanceof Integer) {
                build.withInt(next.getKey(), (int) next.getValue());
            } else if (obj instanceof Long) {
                build.withLong(next.getKey(), (long) next.getValue());
            } else if (obj instanceof Float) {
                build.withFloat(next.getKey(), (float) next.getValue());
            } else if (obj instanceof Double) {
                build.withDouble(next.getKey(), (double) next.getValue());
            } else if (obj instanceof Byte) {
                build.withByte(next.getKey(), (byte) next.getValue());
            } else if (obj instanceof Boolean) {
                build.withBoolean(next.getKey(), (Boolean) next.getValue());
            } else if (obj instanceof Character) {
                build.withChar(next.getKey(), (char) next.getValue());
            } else if (obj instanceof Bundle) {
                build.withBundle(next.getKey(), (Bundle) next.getValue());
            } else if (obj instanceof Serializable) {
                build.withSerializable(next.getKey(), (Serializable) next.getValue());
            } else if (obj instanceof Parcelable) {
                build.withParcelable(next.getKey(), (Parcelable) next.getValue());
            }
        }
    }

}
